const {
  getCoachList,
  getCoachDetailById,
  getCoachDetailByUsername,
  updateCoach,
  getCoachNameList, delCoach, newCoach, updateCoachReverseCar, cancelReverseCar
} = require('../controller/coach')
const {getCarDetail, updateCar, cancelUpdateCar} = require("../controller/car");
const {getStudentInformation} = require("../controller/student");
const {newAccount, deleteAccountByUsername} = require("../controller/user");
const {getArrangeByUsername} = require("../controller/arrange");
const {getReverseListByCoachId} = require("../controller/reverse");
const {SuccessModel, ErrorModel} = require('../model/response')
var express = require('express');
var router = express.Router();

/**
 * 管理员路由操作
 * StudentUser.vue: /list/name
 * Cars.vue: /list
 * CoachUser.vue: /list, /new, /update, /del/:id
 * Teaching.vue: /list
 */
// 获取所有教练
router.post('/list', (req, res, next) => {
  const result = getCoachList()
  return result.then(rows => {
    res.json(new SuccessModel(rows))
  })
})
// 添加教练，创建默认账户
router.post('/new', async (req, res, next) => {
  const CoachResult = await newCoach(req.body.coach)
  if (CoachResult) {
    const UserResult = await newAccount(req.body.account)
    if (UserResult) {
      res.json(new SuccessModel('添加学员成功'))
    }
  } else {
    res.json(new ErrorModel('添加学员失败'))
  }
})
// 更新教练 phone,progress
router.post('/update', (req, res, next) => {
  const result = updateCoach(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('修改教练信息成功'))
    } else {
      res.json(new ErrorModel('修改教练信息失败'))
    }
  })
})
// 删除教练，同时删除账户
router.post('/del/:id', async (req, res, next) => {
  const CoachResult = await delCoach(req.params.id)
  if (CoachResult) {
    const UserResult = await deleteAccountByUsername(req.body.identify)
    if (UserResult) {
      res.json(new SuccessModel('删除学员成功'))
    }
  } else {
    res.json(new ErrorModel('删除学员失败'))
  }
})
// 获取教练 name,id --> 供学员选择(添加学员)
router.get('/list/name', (req, res, next) => {
  const result = getCoachNameList()
  return result.then(rows => {
    res.json(new SuccessModel(rows))
  })
})

/**
 * 教练路由操作
 * Information.vue: /update, /detail/username
 * CoachStudent.vue: /detail/username
 * CarReverse.vue: /reverse, /reverse/cancel, /detail/car
 * Schedule.vue: /arrange/username, /list/reverse
 */
// 根据 username 查询教练信息 -- 教练操作
router.post('/detail/username', (req, res, next) => {
  const result = getCoachDetailByUsername(req.session.username)
  return result.then(row => {
    if (row.identify) {
      res.json(new SuccessModel(row))
    } else {
      res.json(new ErrorModel('获取教练详细信息失败'))
    }
  })
})
// 获取教练预约车辆信息
router.get('/detail/car', async (req, res, next) => {
  // 获取教练预约车辆信息
  const CoachInfo = await getCoachDetailByUsername(req.session.username)
  if (CoachInfo.reverse_car) {
    const CarInfo = await getCarDetail(CoachInfo.reverse_car)
    if (CarInfo.car_number) {
      res.json(new SuccessModel(CarInfo))
    } else {
      res.json(new ErrorModel())
    }
  } else {
    res.json(new ErrorModel())
  }
})
// 教练预约车辆
router.post('/reverse', async (req, res, next) => {
  const val = await updateCoachReverseCar(req.session.username, req.body.car_number)
  if (val) {
    const CarVal = await updateCar(req.body)
    if (CarVal) {
      res.json(new SuccessModel('预约车辆成功'))
    }
  } else {
    res.json(new ErrorModel('预约车辆失败'))
  }
})
// 教练取消预约车辆
router.post('/reverse/cancel', async (req, res, next) => {
  const val = await cancelReverseCar(req.session.username)
  if (val) {
    const CarVal = await cancelUpdateCar(req.body)
    if (CarVal) {
      res.json(new SuccessModel('取消预约车辆成功'))
    }
  } else {
    res.json(new ErrorModel('取消预约车辆失败'))
  }
})
// 获取教练练车时间安排
router.post('/arrange/username', async (req, res, next) => {
  const CoachInfo = await getCoachDetailByUsername(req.session.username)
  const ArrangeInfo = await getArrangeByUsername(req.session.username)
  res.json(new SuccessModel({
    identify: CoachInfo.identify,
    name: CoachInfo.name,
    isArranged: CoachInfo.isArranged,
    ...ArrangeInfo
  }))
})
// 获取预约情况
router.get('/list/reverse', async (req, res, next) => {
  const CoachInfo = await getCoachDetailByUsername(req.session.username)
  const ArrangeInfo = await getArrangeByUsername(req.session.username)
  const Students = getReverseListByCoachId(CoachInfo.id)
  return Students.then(rows => {
    res.json(new SuccessModel({
      number_limit: ArrangeInfo.number_limit,
      data: rows
    }))
  })
})

/**
 * 学员路由操作
 * CoachInfo.vue: /detail
 * AppointTrain.vue: /arrange
 */
// 根据学员 identify 获取教练时间安排
router.post('/arrange', async (req, res, next) => {
  // 1. 获取学员信息
  const StudentInfo = await getStudentInformation(req.session.username)
  // 2. 获取教练信息
  const CoachInfo = await getCoachDetailById(StudentInfo.coach_id)
  // 3. 获取练车时间安排
  const ArrangeInfo = await getArrangeByUsername(CoachInfo.identify)
  res.json(new SuccessModel({
    coach_id: CoachInfo.id,
    ...ArrangeInfo
  }))
})
// 根据学员的 coach_id 查询教练信息 -- 学员操作
router.post('/detail', (req, res, next) => {
  const StudentInfoResult = getStudentInformation(req.session.username)
  StudentInfoResult.then(info => {
    const result = getCoachDetailById(info.coach_id)
    return result.then(row => {
      if (row.identify) {
        res.json(new SuccessModel(row))
      } else {
        res.json(new ErrorModel('获取教练详细信息失败'))
      }
    })
  })
})


module.exports = router;
